import { chargeApi } from "@/api/MedicalExamination/outpatientExpenses/charge";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicColumn, BasicTable, useTable } from "@/components/Table";
import { Descriptions, message, Select } from "antd";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import "./index.less";
import { formatDate } from "@/utils/util";
import { bookkeepApi } from "@/api/statisticalReport/financialTransactions";
import { ApiSelect } from "@/components/Form";
const { Item } = Descriptions;
let getChargeList: Function;
const TreatmentPhmList = (props: { register: Fn; success?: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "MedicalExamination.outpatientExpenses.charge";
	const [orderId, setOrderId] = useState("");
	const [patCardno, setPatCardno] = useState("");
	const [debitAccount, setDebitAccount] = useState("");
	const [pkId, setPkId] = useState("");
	const [page, setPage] = useState("1");
	const [information, setInformation] = useState<any>({});
	const [data, setData] = useState<any>([]);
	const [totalAmount, setTotalAmount] = useState<any>(0);
	const [totalDiscounts, setTotalDiscounts] = useState<any>(0);
	const options = [
		{
			label: t("MedicalExamination.outpatientExpenses.charge.cash"),
			value: "1"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.socialSecurity"),
			value: "2"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.bankCard"),
			value: "3"
		},
		{
			label: t("MedicalExamination.outpatientExpenses.charge.others"),
			value: "4"
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.serviceName`),
			dataIndex: "bizName"
		},
		{
			title: t(`${en_vi}.quantity`),
			dataIndex: "bizQty"
		},
		{
			title: t(`${en_vi}.amount`),
			dataIndex: "bizPrice"
		},
		{
			title: t(`${en_vi}.discountAmountYuan`),
			dataIndex: "discountPrice"
		},
		{
			title: t(`${en_vi}.discountRatePercent`),
			dataIndex: "discountPercent"
		},
		{
			title: t(`${en_vi}.actualPaymentYuan`),
			dataIndex: "discountPercent",
			render: (_: any, record: any) => record.bizPrice - record.discountPrice
		},
		{
			title: t(`${en_vi}.creationTime`),
			dataIndex: "createdTime",
			render: (time: any) => formatDate(new Date(time))
		}
	];
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		console.log(data);
		setPkId(data.record.pkId);
		setInformation(data.information);
		setOrderId(data.record.orderId);
		setPatCardno(data.information.patCardno);
		getChargeList = data.getChargeList;
	});
	const [register] = useTable({
		dataSource: data,
		columns,
		useSearchForm: false,
		bordered: false
	});
	const handleSubmit = async () => {
		const res = await chargeApi.chargeUpdate({
			pkId,
			status: 1
		});
		if (!res) return message.error(t(`${en_vi}.refundFailed`));
		message.success(t(`${en_vi}.refundSuccessful`));
		console.log(patCardno);
		await bookkeepApi
			.bookkeepCreate({
				debitAccount,
				creditAccount: patCardno,
				creditAmount: totalAmount - totalDiscounts,
				debitAmount: totalAmount - totalDiscounts
			})
			.then(res => {
				console.log(res);
			});
		closeDrawer();
		getChargeList();
		setOrderId("");
		setPkId("");
	};
	const handleChange = (value: string) => {
		setPage(value);
	};
	const apiChange = (value: any) => {
		setDebitAccount(value);
	};
	const getCharge = async () => {
		const res = await chargeApi.chargeList({
			orderId
		});
		let totlaPrice = 0;
		let totlaDis = 0;
		res.list.forEach((item: any) => {
			totlaPrice += item.bizPrice;
			totlaDis += item.discountPrice;
		});
		setTotalAmount(totlaPrice);
		setTotalDiscounts(totlaDis);
		setData(res.list);
	};
	useEffect(() => {
		if (!orderId) return;
		getCharge();
	}, [orderId]);
	return (
		<div>
			<BasicDrawer showFooter={true} size="large" {...props} register={registerDrawer} ok={handleSubmit}>
				<div className="charge_details">
					<div>
						{information ? (
							<Descriptions column={1} layout="horizontal">
								<Item label={t("MedicalExamination.orderDetails.orderNumber")}>{orderId}</Item>
								<Item label={t("MedicalExamination.orderDetails.name")}>{information.fullName}</Item>
								<Item label={t("MedicalExamination.orderDetails.age")}>{information.age}</Item>
								<Item label={t("MedicalExamination.orderDetails.gender")}>{information.gender}</Item>
								<Item label={t("MedicalExamination.orderDetails.ethnicity")}>{information.ethnicity}</Item>
								<Item label={t("MedicalExamination.orderDetails.dateOfBirth")}>{information.birthday}</Item>
							</Descriptions>
						) : (
							""
						)}
					</div>
					<BasicTable register={register}></BasicTable>
					<div>
						<Descriptions column={3} layout="horizontal">
							<Item label={t("MedicalExamination.orderDetails.paymentType")} span={24}>
								<Select defaultValue={page} style={{ width: 120 }} onChange={handleChange} options={options} />
							</Item>
							<Item label={t("MedicalExamination.outpatientExpenses.charge.receivingAccount")} span={24}>
								<ApiSelect
									api={chargeApi.accountsList}
									labelField="accountNo"
									valueField="accountNo"
									onChange={apiChange}
								></ApiSelect>
							</Item>
							<Item label={t(`${en_vi}.totalAmount`)}>{totalAmount}</Item>
							<Item label={t(`${en_vi}.totalDiscount`)}>{totalDiscounts}</Item>
							<Item label={t(`${en_vi}.totalActualPayment`)}>{totalAmount - totalDiscounts}</Item>
						</Descriptions>
					</div>
				</div>
			</BasicDrawer>
		</div>
	);
};

export default TreatmentPhmList;
